package com.spark.modules.system.controller;

import com.spark.common.core.Result;
import com.spark.common.core.api.SparkCommonApi;
import com.spark.common.core.entity.model.SelectTableModel;
import com.spark.common.core.entity.model.SelectTreeTableModel;
import com.spark.common.core.entity.params.SelectTableParams;
import com.spark.common.core.entity.params.SelectTableTreeParams;
import com.spark.common.core.entity.params.ValidExistDataParams;
import com.spark.common.core.entity.system.SysParams;
import com.spark.common.upload.StorageService;
import com.spark.common.utils.ParamsUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统级控制器
 *
 * @author LYCHEE
 * @date 2025/2/20 09:32
 */
@RestController
@RequestMapping(value = "/sys")
public class SystemController {

    @Resource
    private SparkCommonApi sparkCommonApi;

    @Resource
    private StorageService storageService;

    /**
     * 查询表中数据
     *
     * @param params 参数
     * @return 结果
     */
    @GetMapping(value = "/querySelectTable")
    public Result<?> querySelectTable(@Validated SelectTableParams params) {
        List<SelectTableModel> tableModelList = sparkCommonApi.querySelectTable(params);
        return Result.ok(tableModelList);
    }

    /**
     * 查询树表中数据
     *
     * @param params 参数
     * @return 结果
     */
    @GetMapping(value = "/querySelectTreeTable")
    public Result<?> querySelectTreeTable(@Validated SelectTableTreeParams params) {
        List<SelectTreeTableModel> tableModelList = sparkCommonApi.querySelectTreeTable(params);
        return Result.ok(tableModelList);
    }

    /**
     * 验证是否存在数据
     *
     * @param params 参数
     * @return 是否存在
     */
    @GetMapping(value = "/validExistData")
    public Result<?> validExistData(@Validated ValidExistDataParams params) {
        boolean isExistData = sparkCommonApi.validExistData(params);
        return Result.ok(isExistData);
    }

    /**
     * 根据key获取一组配置参数
     *
     * @param key key
     * @return 参数
     */
    @GetMapping(value = "/getGroupParamsByKey")
    public Result<?> getGroupParamsByKey(@RequestParam String key) {
        List<SysParams> groupParamsByKey = ParamsUtils.getGroupParamsByKey(key);
        return Result.ok(groupParamsByKey);
    }

    /**
     * 根据配置的字段获取输入的字
     * @param fieldNames 字段名称
     * @return 字段数据
     */
    @GetMapping(value = "/getParamsByFieldName")
    public Result<?> getParamsByFieldName(@RequestParam String fieldNames){
        Map<String, String> params = new HashMap<String, String>();
        String[] split = fieldNames.split(",");
        for (String field : split) {
            String inputVal = ParamsUtils.getInputValByConfigField(field);
            params.put(field, inputVal);
        }
        return Result.ok(params);
    }

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 上传提示信息
     */
    @PostMapping(value = "/upload")
    public Result<?> upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail("请上传文件");
        }
        try {
            String fileUrl = storageService.upload(file);
            return Result.ok("上传成功", fileUrl);
        } catch (IOException e) {
            return Result.fail("上传失败: " + e.getMessage());
        }
    }
}
